/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webidewar.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author huy
 */
public class ProcessesManager {

    public static ArrayList<Process> procs = new ArrayList<Process>();

    public String input(int _id, String msg) {
        Process p = getProcessById(_id);
        PrintStream ps = new PrintStream(p.getOut());
        ps.println(msg);
        
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getIn()));
        String re = "";
        try {
            re = br.readLine();
        } catch (IOException ex) {
            Logger.getLogger(ProcessesManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }

    public void print(int _id, String msg) {
        Process p = getProcessById(_id);
        PrintStream ps = new PrintStream(p.getOut());
        ps.println(msg);
    }
    
    private Process getProcessById(int _id){
        Process re=null;
        for (int i=0;i<procs.size();i++){
            if (procs.get(i).getId()==_id)
                re = procs.get(i);
        }
        return re;
    }
    
    public static void test(String t){
        Logger.getLogger(ProcessesManager.class.getName()).log(Level.SEVERE, null,"\n>>>>>"+procs.get(0).getId()+">>>>>>>>>>>>>>>\n");

    }
}
